<template>
  <el-dialog
    title="选择商品"
    :visible.sync="dialogVisible"
    width="845px"
    :before-close="handleClose"
    class="material"
    style="user-select: none;"
  >
    <div class="clearfix">
      <div
        v-if="this.$store.state.app_id == 'Zm9n'"
        style="    height: 50px;
    width: 100%;
    padding: 0 20px;
    line-height: 40px; overflow-x: auto;
        overflow-y:hidden ; display:flex;"
      >
        <div
          v-for="(item, index) in typeIdicts"
          :key="index"
          style="height: 100%;   cursor:pointer;  text-align: center;
    padding:0 10px; "
          :style="index == typeidx ? 'color:#409EFF' : ''"
          @click="typeTags(item.id, index)"
        >
          {{ item.name }}
        </div>
      </div>
      <div
        class="img_box"
        style="background:#fff;margin:10px 22px;padding-bottom:5px;height:290px;"
      >
        <div
          v-for="(item, index) in getImgList"
          :key="index"
          class="img_item_border"
          :style="{ borderColor: item.check ? '#3587fc' : '' }"
          @click="checkHandle(img_index)"
        >
          <img :src="item.src" />
          <div style="height:63px;position:relative;">
            <div style=" width: 66px; height: 42px; overflow: hidden;">
              {{ item.goods_name }}
            </div>
            <div style="position:absolute;bottom:0px;">
              {{ item.low_goods_price }}
            </div>
          </div>
        </div>
        <div
          v-if="getImgList.length <= 0"
          style="text-align:center;margin:150px 0;"
        >
          您暂未添加商品
        </div>
      </div>
    </div>
    <div>
      <div style="text-align:center;padding-top:10px;">
        <el-pagination
          small
          background
          layout="pager,jumper"
          :page-size="15"
          :current-page="pages"
          :total="listLenght"
          @current-change="changes"
        >
        </el-pagination>
      </div>
      <div style="text-align:right;padding-right:20px;padding-bottom:20px;">
        <el-button size="small" @click="handleClose">取 消</el-button>
        <el-button size="small" type="primary" @click="addImg">确 定</el-button>
      </div>
    </div>
    <!-- </div> -->
  </el-dialog>
</template>

<script>
import { GOODSLISTShop } from "@/service/shop";
export default {
  name: "WareAlert",
  props: {
    ware: {
      type: Boolean,
      default: false
    },
    idx: {
      type: Number,
      required: true
    },
    usename: {
      type: String,
      default: ""
    },
    typeIdicts: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      listLenght: 0,
      group_id: "",
      dialogVisible: this.ware,
      imgList: [],
      input5: "",
      getImgList: [],
      typeData: [],
      pages: 1,
      cont: "",
      typeid: "",
      typeidx: -1
    };
  },
  watch: {
    ware(val) {
      this.dialogVisible = val;
      if (val) {
        this.changeCurrentPage();
      }
    }
  },
  created() {
    this.cont = "";
  },
  methods: {
    changes(page) {
      this.pages = page;
      this.changeCurrentPage(page);
    },
    typeTags(id, idx) {
      this.typeid = id;
      this.typeidx = idx;
      this.changeCurrentPage();
    },

    async changeCurrentPage(val) {
      this.getImgList = [];
      let params = {
        page: val || 1,
        pageshow: 15,
        type_id: this.typeid
      };
      await GOODSLISTShop(params).then(({ code, data }) => {
        if (code === 200) {
          this.listLenght = data.length;
          data.list.forEach(item => {
            let jsons = {
              goods_name: item.goods_name,
              goods_price: item.goods_price,
              low_goods_price: item.low_goods_price,
              goods_type: item.goods_type,
              goods_origin_price: item.low_goods_price,
              check: false,
              src: this.$store.state.qiniu + item.goods_cover.split(",")[0],
              goods_id: item.goods_id
            };
            this.getImgList.push(jsons);
          });
        }
      });
      this.idxTag();
    },
    idxTag() {
      if (this.idx === 1) {
        this.cont = "";
        this.getImgList.forEach(ele => {
          ele.check = false;
        });
      }
    },
    handleClose() {
      this.dialogVisible = false;
      this.$emit("close");
    },
    checkHandle(i) {
      if (this.idx === 1) {
        if (this.cont !== "") {
          this.getImgList[this.cont].check = !this.getImgList[this.cont].check;
        }
        this.getImgList[i].check = !this.getImgList[i].check;

        this.cont = i;
      } else {
        this.getImgList[i].check = !this.getImgList[i].check;
      }
    },
    addImg() {
      let list = this.getImgList.filter(item => {
        return item.check === true;
      });
      // this.cont = '';
      if (this.usename === "菜品") {
        if (list.length >= 3) {
          this.$emit("postImgs", list);
          this.$emit("close");
          this.dialogVisible = false;
        } else {
          this.$message.error("至少选择3个菜品！");
        }
      } else {
        this.$emit("postImgs", list);
        this.$emit("close");
        this.dialogVisible = false;
      }
    }
  }
};
</script>

<style scoped lang="scss">
.el-pagination {
  white-space: initial;
}
.currentImg {
  border: 2px solid #00ffff !important;
}
.form-select {
  width: 100px;
  transform-origin: left;
  margin-left: 20px;
}
.form-radio {
  font-size: 12px;
  transform-origin: left;
}
.form-inline-form {
  width: 200px;
  display: flex;
  flex-direction: row;
  align-items: top;
  justify-content: space-around;
  display: inline-block;
  margin-left: 160px;
  .form-inline-form-from {
    width: 80px;
  }
  .form-inline-form-to {
    width: 80px;
  }
}
.form-search {
  width: 200px;
  margin-left: 20px;
}
.form-input1 {
  position: relative;
  &::before {
    content: "价格";
    font-size: 12px;
    left: -49px;
    position: absolute;
  }
}
.form-input2 {
  position: relative;
  margin-left: 20px;
  &::before {
    content: "--";
    font-size: 12px;
    left: -25px;
    display: block;
    position: absolute;
    color: grey;
  }
}
.img_box {
  // position: absolute;
  // width: 96%;
  // height: 360px;
  // overflow-y: scroll;
  // display: flex;
  // flex-wrap: wrap;
  // justify-content: flex-start;
  // float: left;
  // z-index: 99;
  // margin: 5px auto;
  .img_item_border {
    float: left;
    z-index: 999999999;
    border: 1px solid rgba(grey, 0.4);
    height: 90px;
    width: 150px;
    margin: 5px 5px 0px;
    cursor: pointer;
    z-index: 99;
    border-radius: 5px;
    display: flex;
    align-items: center;
    img {
      width: 63px;
      height: 63px;
      // margin-top: 15%;
      border-radius: 5px;
      // border: 1px solid rgba(grey,0.4);
      margin-left: 5px;
    }
    div {
      margin-left: 3px;
      color: rgba(grey, 0.8);
      text-align: left;
    }
  }
}

.clearfix:before,
.clearfix:after {
  content: " ";
  display: table;
}
.clearfix:after {
  clear: both;
}
</style>
